package com.gitee.qdbp.jdbc.plugins.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.gitee.qdbp.able.jdbc.condition.TableJoin;
import com.gitee.qdbp.able.jdbc.condition.TableJoin.JoinItem;
import com.gitee.qdbp.able.jdbc.condition.TableJoin.TableItem;
import com.gitee.qdbp.jdbc.model.AllFieldColumn;
import com.gitee.qdbp.jdbc.model.SimpleFieldColumn;
import com.gitee.qdbp.jdbc.model.TablesFieldColumn;
import com.gitee.qdbp.jdbc.plugins.DbPluginHelper;
import com.gitee.qdbp.jdbc.plugins.TablesFieldColumnParser;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 多表关联列信息的解析器
 *
 * @author zhaohuihua
 * @version 20210306
 */
public class SimpleTablesFieldColumnParser implements TablesFieldColumnParser, DbPluginHelper.Aware {

    /** 插件容器 **/
    protected DbPluginHelper plugins;

    private String tableAliasSeparator = "__";

    @Override
    public List<TablesFieldColumn> parseFieldColumns(TableJoin tables) {
        TableItem major = tables.getMajor();
        List<JoinItem> joins = tables.getJoins();
        List<TablesFieldColumn> all = new ArrayList<>();
        { // 添加主表的字段
            List<TablesFieldColumn> fields = scanColumnList(major);
            all.addAll(fields);
        }
        if (VerifyTools.isNotBlank(joins)) {
            // 添加关联表的字段
            for (JoinItem item : joins) {
                List<TablesFieldColumn> fields = scanColumnList(item);
                all.addAll(fields);
            }
        }
        // 处理重名字段: 设置columnAlias
        // 1.先统计字段出现次数
        Map<String, Integer> countMaps = new HashMap<>();
        for (SimpleFieldColumn field : all) {
            String fieldName = field.getFieldName();
            if (countMaps.containsKey(fieldName)) {
                countMaps.put(fieldName, countMaps.get(fieldName) + 1);
            } else {
                countMaps.put(fieldName, 1);
            }
        }
        // 2.如果出现多次则设置columnAlias=tableAlias__columnName
        for (TablesFieldColumn field : all) {
            String fieldName = field.getFieldName();
            if (countMaps.get(fieldName) > 1) {
                String tableAlias = field.getTableAlias().toUpperCase();
                String columnName = field.getColumnName();
                field.setColumnAlias(tableAlias + tableAliasSeparator + columnName);
                field.setAmbiguous(true);
            }
        }
        return all;
    }

    private List<TablesFieldColumn> scanColumnList(TableItem table) {
        AllFieldColumn<SimpleFieldColumn> all = plugins.parseAllFieldColumns(table.getTableType());
        String tableAlias = table.getTableAlias();
        String resultField = table.getResultField();
        List<SimpleFieldColumn> fields = all.items();
        List<TablesFieldColumn> result = new ArrayList<>(fields.size());
        for (SimpleFieldColumn item : fields) {
            TablesFieldColumn copied = item.to(TablesFieldColumn.class);
            copied.setTableAlias(tableAlias);
            copied.setResultField(resultField);
            result.add(copied);
        }
        return result;
    }

    public void setTableAliasSeparator(String tableAliasSeparator) {
        this.tableAliasSeparator = tableAliasSeparator;
    }

    @Override
    public String getTableAliasSeparator() {
        return tableAliasSeparator;
    }

    /** 插件容器 **/
    @Override
    public void setPlugins(DbPluginHelper plugins) {
        this.plugins = plugins;
    }
}
